package com.bezdelnick.forum.dao.hibernate;

import com.bezdelnick.forum.dao.ITypeDao;
import com.bezdelnick.forum.domain.Type;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Property;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 18.07.12
 * Time: 12:44
 * To change this template use File | Settings | File Templates.
 */
public class TypeDaoImpl implements ITypeDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(ITypeDao.class);

    @Autowired(required = true)
    private SessionFactory sessionFactory;

    @Override
    public List<Type> loadTypes() {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            return session.createCriteria(Type.class).list();
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return null;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public Type getTypeById(long id) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            return (Type) session.get(Type.class, id);
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return null;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public Type getTypeByName(String name) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            Type type = (Type) session.createCriteria(Type.class).add(Property.forName("name").eq(name)).list().get(0);
            return type;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return null;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public boolean insertType(Type type) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            session.save(type);
            return true;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return false;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public boolean updateType(Type type) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            session.update(type);
            return true;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return false;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public boolean deleteType(long id) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            Type type = new Type();
            type.setId(id);
            session.delete(type);
            return true;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return false;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }
}
